package com.itheima.health.controller.backend;

import com.itheima.health.constant.MessageConstant;
import com.itheima.health.constant.RedisMessageConstant;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.User;
import com.itheima.health.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private StringRedisTemplate stringredisTemplate;
    /**
     * 用户登录
     * @param registerUser
     * @return
     */
    @RequestMapping("/register.do")
    public Result register(@RequestBody User registerUser) {
        // 调用业务层注册
        Result result = userService.register(registerUser);

        // 响应统一结果
        return result;
    }
    /**
     * 用户登录
     * @param loginUser
     * @return
     */
    @RequestMapping("/login.do")
    public Result login(@RequestBody User loginUser,HttpServletRequest request) {
        // 调用业务层登录
        HttpSession session = request.getSession();
        Result result = userService.login(loginUser, session);
        // 响应统一结果
        return result;
    }

    /**
     * 用户使用电话号码登录
     * @return
     */
    @PostMapping("telephoneLogin.do")
    public Result telephoneLogin(@RequestBody Map<String, String> map, HttpServletRequest request){
        // 1.获取用户输入的手机号和验证码
        String telephone = map.get("telephone");
        String validateCode = map.get("smsCode");

        // 2.从Redis中获取缓存的验证码
        String authCode = stringredisTemplate.opsForValue().get(telephone+RedisMessageConstant.SENDTYPE_LOGIN);

        // 3.如果Redis中没有验证码,提示用户验证码已过期
        if (authCode == null) {
            return new Result(false, MessageConstant.SEND_VALIDATECODE_OUTTIME);
        }
        // 4.如果验证码不正确，提示用户验证码不正确
        if (!authCode.equals(validateCode)) {
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }

        // 5.如果验证码正确，则调用dao层方法根据手机号查询
        User user = userService.findByTelephone(telephone);
        // 6.判断当前用户是否为会员，如果不是会员则自动完成会员注册
        if (user == null) {
            return new Result(false, "登录失败, 手机号不存在");
        }
//        stringredisTemplate.delete(telephone + RedisMessageConstant.SENDTYPE_BACKEND_GETPWD);
        else{
            HttpSession session = request.getSession();
            // 7.响应统一结果
            session.setAttribute("loginUser",user);
            stringredisTemplate.delete(telephone + RedisMessageConstant.SENDTYPE_GETPWD);
            return new Result(true, MessageConstant.LOGIN_SUCCESS);}
    }
}